<template>
  <el-dialog
    title="awc计算"
    :visible.sync="dialogVisible"
    width="1000px"
  >
    <div class="app-container">
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="80px" class="demo-ruleForm">
        <el-form-item label="粉粒" prop="silt">
          <el-input v-model="ruleForm.silt" type="number" placeholder="粉粒" clearable />
        </el-form-item>
        <el-form-item label="砂粒" prop="sand">
          <el-input v-model="ruleForm.sand" type="number" placeholder="砂粒" clearable />
        </el-form-item>
        <el-form-item label="黏粒" prop="clay">
          <el-input v-model="ruleForm.clay" type="number" placeholder="黏粒" clearable />
        </el-form-item>
        <el-form-item label="容重" prop="unit_weight">
          <el-input v-model="ruleForm.bd" type="number" placeholder="容重" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loading" @click="submitForm('ruleForm')">计算</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
      <div v-if="awcData && awcData.residual_water_content">
        <div class="h12" />
        <div class="tip" style="padding:16px">
          <el-descriptions title="计算结果" border>
            <el-descriptions-item label="田间持水量">{{ awcData.residual_water_content }}</el-descriptions-item>
            <el-descriptions-item label="凋萎含水率">{{ awcData.saturated_water_content }}</el-descriptions-item>
            <el-descriptions-item label="AWC">{{ awcData.residual_water_content - awcData.saturated_water_content }}</el-descriptions-item>
          </el-descriptions>
        </div>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import { awc } from '@/api/util'
export default {
  data() {
    return {
      dialogVisible: false,
      loading: false,
      awcData: {},
      ruleForm: {
        silt: '',
        sand: '',
        clay: '',
        bd: ''
      },
      rules: {
        silt: [
          { required: true, message: '请输入粉粒', trigger: 'blur' }
        ],
        sand: [
          { required: true, message: '请输入砂粒', trigger: 'blur' }
        ],
        clay: [
          { required: true, message: '请输入黏粒', trigger: 'blur' }
        ],
        bd: [
          { required: true, message: '请输入容重', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    show() {
      this.dialogVisible = true
    },
    awc() {
      this.loading = true
      awc(this.ruleForm).then((res) => {
        this.awcData = res.data
        this.loading = false
      }).catch((err) => {
        console.log(err)
        this.loading = false
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.awc()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.ruleForm = {
        silt: '',
        sand: '',
        clay: '',
        bd: ''
      }
      this.awcData = {}
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
